//
//  SNPlanListTopView.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/25.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNPlanListTopView.h"
#import "UIImageView+WebCache.h"
#import "NSDate+TimeStamp.h"
#import "UIImageView+TapGesture.h"
@interface SNPlanListTopView()
@property (weak, nonatomic) IBOutlet UIImageView *arrowImageView;

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;

/** selected */
@property (nonatomic, assign,getter=isSelected) BOOL selected;
/** calendarImageView */
@property (weak, nonatomic) UIImageView * calendarImageView;
/** 年度列表 */
@property (nonatomic,strong) NSMutableArray<NSString *> * strings;

@end
@implementation SNPlanListTopView
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self addRequest];
    }
    return self;
}
- (void)addRequest
{
    [[ZXHTTPManager sharedManager] queryplannameWithToken:@"" timestamp:@"" completed:^(NSDictionary *jsonDict, NSError *error) {
        
        logError;
        
        NSLog(@"%@",jsonDict);
        
        NSArray<NSDictionary *> * planNames =  jsonDict[@"result"];
        
        NSMutableArray<NSString *> * strings = @[].mutableCopy;
        [planNames enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSString * planName =  obj[@"planName"];
            [strings addObject:planName];
        }];
        
        self.strings = strings;
        
        //                              /*轻触 UITapGestureRecognizer*/
        /*****************************************************************************************/
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapActived:)];
        //        tap.delegate = self;
        //        tap.numberOfTapsRequired = 2; // 点几次
        //        //	tap.numberOfTouchesRequired = 2; // 几根手指
        self.planNameLable.userInteractionEnabled = YES;
        [self.planNameLable addGestureRecognizer:tap];
        
        @weakify_self
        self.arrowImageView.sn_tapBlock = ^(){
            @strongify_self
            [self tapActived:nil];
        };
    }];
}
- (void)tapActived:(UITapGestureRecognizer *)sender {
    
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    [self.superview addSubview:bgView];
    
    
    // NSLog(@"%@",@111111111111111111);
    self.selected = !self.selected;
    
    if (self.isSelected) {
        
        [self.calendarImageView removeFromSuperview];
        self.arrowImageView.layer.affineTransform = CGAffineTransformMakeRotation(M_PI);
        UIImageView * calendarImageView = ({
            UIImageView * imageView = [[UIImageView alloc] init];
            imageView.userInteractionEnabled = YES;
            UIImage * image = [UIImage imageNamed:@"01-year-select"];
            imageView.image = image;
            
            
            CGRect frame = [self convertRect:self.planNameLable.frame toView:self.superview];
            NSLog(@"frame = %@",NSStringFromCGRect(frame));
            //@[@"2014年度生产计划",@"2015年度生产计划",@"2016年度生产计划",@"2016年度生产计划",@"2016年度生产计划",@"2016年度生产计划"];
            
            NSArray <NSString *>* strings = self.strings;
            
            
            imageView.frame = CGRectMake(frame.origin.x-8, CGRectGetMaxY(frame), (self.planNameLable.frame.size.width + 8+11+20), 40 * strings.count);
            [self.superview addSubview:imageView];
            [self addCanvasView:imageView.frame parentView:imageView strings:strings bgView:bgView];
            imageView;
        });
        self.calendarImageView = calendarImageView;
    }else{
        [self.calendarImageView removeFromSuperview];
        
    }
    
}

-(void)awakeFromNib{
    [super awakeFromNib];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        self.iconImageView.layer.cornerRadius = self.iconImageView.bounds.size.width * 0.5;
        self.iconImageView.layer.masksToBounds = YES;
    });
    self.layer.contents = (__bridge id)[UIImage imageNamed:@"01-list-base"].CGImage;
    self.backgroundColor = kNormalBackgroundColor;
    
    self.nameLabel.text = [NSString stringWithFormat:@"%@ | %@",kUserInfo.userName,kUserInfo.fullName];
    // self.planNameLable.text = [NSString stringWithFormat:@"%@年度生产计划",[NSDate new].dateToString(@"yyyy")];
}
- (void)addCanvasView:(CGRect)frame parentView:(UIView *)parentView strings:(NSArray<NSString *>*)strings bgView:(UIView *)bgView
{
    UIView * view = [[UIView alloc] init];
    
    view.backgroundColor = [UIColor clearColor];
    
    view.frame = CGRectMake(8, 8, frame.size.width - 8 -11, frame.size.height-8-12);
    
    [parentView addSubview:view];
    
    for(int i = 0; i < strings.count; i++) {
        
        UILabel * yearLabel = [[UILabel alloc] init];
        //------------------------------
        yearLabel.numberOfLines = 0;
        yearLabel.textColor = [UIColor blackColor];
        yearLabel.textAlignment = NSTextAlignmentCenter;
        yearLabel.text = strings[i];
        yearLabel.font = [UIFont systemFontOfSize:13];
        
        yearLabel.frame = CGRectMake(0, 0 + i * view.frame.size.height / strings.count, view.frame.size.width, view.frame.size.height / strings.count);
        
        @weakify_self
        yearLabel.sn_tapBlock = ^(){
            @strongify_self
            self.planNameLable.text = strings[i];
            self.selected = NO;
            self.arrowImageView.layer.affineTransform = CGAffineTransformMakeRotation(0);
            [parentView removeFromSuperview];
            [bgView removeFromSuperview];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TopViewUpdateYearNotification" object:nil userInfo:nil];
        };
        @weakify(bgView)
        bgView.sn_tapBlock = ^(){
            @strongify(bgView) //strong_tableView
            self.selected = NO;
            self.arrowImageView.layer.affineTransform = CGAffineTransformMakeRotation(0);
            [parentView removeFromSuperview];
            [strong_bgView removeFromSuperview];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TopViewUpdateYearNotification" object:nil userInfo:nil];
        };
        
        [view addSubview:yearLabel];
        
        if (i == strings.count-1) {
            //
        }else{
            UIView * splitLineView = [[UIView alloc] initWithFrame:CGRectMake(7, (i+1) * view.frame.size.height / strings.count , view.frame.size.width - 14, 1)];
            splitLineView.backgroundColor = HexRGB(0xf5f5f5);
            [view addSubview:splitLineView];
        }
    }
}
- (void)configWithModel:(SNPlantingDecisionMakerResult *)model year:(NSString *)year{
    _model = model;
    _year = year;
    self.nameLabel.text = [NSString stringWithFormat:@"%@ | %@",model.custName,model.fullName];
    [SNUtils getImageWithURLString:model.photo imageView:self.iconImageView];
    self.planNameLable.text = year;
}
@end
